﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;
using SyncConfig.Dialog;
using DevComponents.DotNetBar.Controls;
using AppCommon.Controls;
using AppCommon;

namespace SyncConfig.Controls
{
    public partial class AddDenpendencyTable : UserControlBase
    {
        public AddDenpendencyTable()
        {
            InitializeComponent();
            errorProvider1.SetIconAlignment(txtApplyCommand, ErrorIconAlignment.MiddleLeft);
            errorProvider1.SetIconAlignment(txtSelectRowCommand, ErrorIconAlignment.MiddleLeft);
            errorProvider1.SetIconAlignment(txtTableName, ErrorIconAlignment.MiddleLeft);
        }

        bool isNew = false;
        public void BindingNew()
        {
            isNew = true;
            txtTableName.Text = "";
            txtPreApplyTableCommand.Text = "";
            txtPreApplyRowCommand.Text = "";
            txtSelectRowCommand.Text = "";
            txtPrimaryKey.Text = "";
        }
        public void BindingInfo(Node node)
        {
            CurrentNode = node;
            SyncConfigDS.DependencyTableRow row = node.Tag as SyncConfigDS.DependencyTableRow;
            isNew = false;

            txtTableName.Text = row.TableName;
            txtPreApplyRowCommand.Text = row.PreApplyRowCommand;
            txtPreApplyTableCommand.Text = row.PreApplyTableCommand;
            txtSelectRowCommand.Text = row.SelectRowCommand;
            txtApplyCommand.Text = row.ApplyRowCommand;
        }

        private void txtPrimaryKey_ButtonCustomClick(object sender, EventArgs e)
        {
            ChoicePrimaryColumn frmChoice = new ChoicePrimaryColumn();
            try
            {
                frmChoice.TableName = txtTableName.Text;
                frmChoice.OldPrimaryColumn = txtPrimaryKey.Text;
                if (frmChoice.ShowDialog() == DialogResult.OK)
                {
                    txtPrimaryKey.Text = frmChoice.CurrentPrimaryColumn;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                frmChoice.Dispose();
            }

        }

        private void Command_ButtonCustomClick(object sender, EventArgs e)
        {
            ChoiceCommand(sender as TextBoxX, "");
        }
        void ChoiceCommand(TextBoxX txtHold, string des)
        {
            if (string.IsNullOrEmpty(des))
                if (!string.IsNullOrEmpty(txtHold.WatermarkText))
                    des = txtHold.WatermarkText;
            PickProcedure frm = new PickProcedure();
            try
            {
                frm.CurrentCommand = txtHold.Text;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    txtHold.Text = string.Format("[{0}].[{1}]", frm.SelectedRow["Schema"], frm.SelectedRow["Name"]);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                frm.Dispose();
            }
        }

        private void labelLinkX3_Click(object sender, EventArgs e)
        {
            try
            {
                errorProvider1.Clear();
                errorProvider1.Tag = null;
                if (string.IsNullOrEmpty(txtTableName.Text))
                {
                    errorProvider1.Tag = 1;
                    errorProvider1.SetError(txtTableName, "Chưa chọn bảng");
                }
                if (string.IsNullOrEmpty(txtApplyCommand.Text.Trim()))
                {
                    errorProvider1.SetError(txtApplyCommand, "Chưa " + txtApplyCommand.WatermarkText);
                    if (!(MessageBox.Show("Bạn chưa chọn [Apply Command]!\n Bạn có chắc vẫn muốn tiếp tục?", "Xac nhan", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
                    {
                        errorProvider1.Tag = 1;
                    }

                }
                if (string.IsNullOrEmpty(txtSelectRowCommand.Text.Trim()))
                {
                    errorProvider1.SetError(txtSelectRowCommand, "Chưa " + txtSelectRowCommand.WatermarkText);
                    errorProvider1.Tag = 1;
                }
                if (errorProvider1.Tag != null)
                {
                    MessageBox.Show("Lỗi nhập liệu");
                    return;
                }
                SyncConfigDS.DependencyTableRow row = CurrentNode.Tag as SyncConfigDS.DependencyTableRow;

                bool flagIsNew = false;
                if (row == null)
                {
                    row = FrmMain.MainForm.TreeManipulation.dsSyncConfig.DependencyTable.NewDependencyTableRow();
                    row.TableId = (CurrentNode.Parent.Tag as SyncConfigDS.TableOfScopeRow).TableId;
                    row.DepenTableId = Guid.NewGuid();
                    flagIsNew = true;

                }

                row.TableName = txtTableName.Text;
                row.ApplyRowCommand = txtApplyCommand.Text;
                row.PreApplyRowCommand = txtPreApplyRowCommand.Text;
                row.PreApplyTableCommand = txtPreApplyTableCommand.Text;
                row.SelectRowCommand = txtSelectRowCommand.Text;
                if (flagIsNew)
                {
                    FrmMain.MainForm.TreeManipulation.dsSyncConfig.DependencyTable.AddDependencyTableRow(row);
                }
                FrmMain.MainForm.TreeManipulation.AdapterDependencyTable.Update(row);

                if (flagIsNew)
                    FrmMain.MainForm.TreeManipulation.BuildDependencyNode(CurrentNode, row.TableOfScopeRow);
                Form frm = FindForm();
                if (frm is BaseDialog)
                {
                    frm.DialogResult = DialogResult.OK;
                }
                else
                    MessageBox.Show("Dữ liệu đã được cập nhật");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtTableName_ButtonCustomClick(object sender, EventArgs e)
        {
            PickTable frm = new PickTable();
            try
            {
                SyncConfigDS.DependencyTableRow row = CurrentNode.Tag as SyncConfigDS.DependencyTableRow;
                Guid tableId;
                if (row != null)
                    tableId = row.TableId;
                else
                {
                    SyncConfigDS.TableOfScopeRow temp = CurrentNode.Parent.Tag as SyncConfigDS.TableOfScopeRow;
                    tableId = temp.TableId;
                }
                frm.WhereCondition = string.Format(" WHERE Name<>'schema_info' AND Name<>'scope_config'" +
                                                    " AND Name<>'scope_info' AND Name<>'scope_templates'" +
                                                    " AND Name<>'scope_parameters' " +
                                                    " AND Name not in (Select TableName from [{0}].[TableOfScope]" +
                                                    " Union all Select TableName from [{0}].[DependencyTable] " +
                                                    " Where TableId='{1}')  AND CHARINDEX('_Tracking',Name)=0"
                                                    , ADOSyncConfig.Schema, tableId);
                frm.Text = "Chon bang dong bo";
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    txtTableName.Text = frm.SelectedRow["Name"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                frm.Dispose();
            }
        }
    }
}
